# -*- coding: utf-8 -*-
# @Time    : 2019/3/1 10:17
# @Author  : shaoeric
# @Email   : shaoeric@foxmail.com

import numpy as np
import matplotlib.pyplot as plt
import cv2
import os

"""
本文件主要完成图片皮肤区域的色彩特征的提取以及统计功能
"""
def get_skin_rgb(file, train=True):
    if train:
        path = 'train'
    else:
        path = 'test'
    img = cv2.imread('{}/origin/{}'.format(path, file))
    ground = cv2.imread('{}/ground/{}'.format(path, file.replace('.jpg', '.png').replace('.jpeg', '.png')), cv2.IMREAD_GRAYSCALE)
    b,g,r = cv2.split(img)

    skin_blue = b[ground > 0]  # 皮肤位置的蓝色值
    skin_red = r[ground > 0]  # 皮肤位置的红色值
    skin_green = g[ground > 0]  # 贫富位置的绿色值

    return skin_red, skin_green, skin_blue


def plot_histogram(skin_red, skin_green, skin_blue):
    plt.subplot(2,2,1)
    plt.hist(skin_red, bins=50,normed=True)
    plt.title('red histogram')

    plt.subplot(2,2,2)
    plt.hist(skin_green, bins=50,normed=True)
    plt.title('green histogram')

    plt.subplot(2,2,3)
    plt.hist(skin_blue, bins=50,normed=True)
    plt.title('blue histogram')
    plt.tight_layout()
    plt.show()

def extract_skin_RGB():
    R, G, B = [], [], []
    i = 0
    for file in os.listdir('train/origin'):
        r,g,b = get_skin_rgb(file)
        R.append(r)
        G.append(g)
        B.append(b)
        i += 1
        print('finish ', i)

    np.save('train/red.npy', np.array(R))
    np.save('train/green.npy', np.array(G))
    np.save('train/blue.npy', np.array(B))

def main():
    R = np.load('train/red.npy')
    G = np.load('train/green.npy')
    B = np.load('train/blue.npy')
    plot_histogram(R, G, B)

main()